\chapter{Robo\+Master电控组通信协议 }
\hypertarget{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2master__machine_2_robf3db6ee153d24cdbb8a8c06ea0d4885b}{}\label{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2master__machine_2_robf3db6ee153d24cdbb8a8c06ea0d4885b}\index{RoboMaster电控组通信协议@{RoboMaster电控组通信协议}}
\label{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2master__machine_2_robf3db6ee153d24cdbb8a8c06ea0d4885b_autotoc_md61}%
\Hypertarget{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2master__machine_2_robf3db6ee153d24cdbb8a8c06ea0d4885b_autotoc_md61}%


可用于视觉，以及其他自研模块（仅限串口通信）

\begin{quote}
TODO\+:


\begin{DoxyEnumerate}
\item 利用\+F4自带的硬件\+CRC模块计算校验码，提高速度
\item 增加更多的数据类型支持，使得数据类型也为可配置的 
\end{DoxyEnumerate}\end{quote}
\hypertarget{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2master__machine_2_robf3db6ee153d24cdbb8a8c06ea0d4885b_autotoc_md62}{}\doxysection{\texorpdfstring{一、串口配置}{一、串口配置}}\label{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2master__machine_2_robf3db6ee153d24cdbb8a8c06ea0d4885b_autotoc_md62}
通信方式是串口，配置为波特率 921600，8 位数据位，1 位停止位，无硬件流控，无校验位。\hypertarget{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2master__machine_2_robf3db6ee153d24cdbb8a8c06ea0d4885b_autotoc_md63}{}\doxysection{\texorpdfstring{二、数据帧说明}{二、数据帧说明}}\label{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2master__machine_2_robf3db6ee153d24cdbb8a8c06ea0d4885b_autotoc_md63}
以下所有低位在前发送，数据长度可变。


\begin{DoxyEnumerate}
\item 通信协议格式
\end{DoxyEnumerate}

\tabulinesep=1mm
\begin{longtabu}spread 0pt [c]{*{4}{|X[-1]}|}
\hline
\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 帧头  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 数据\+ID  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 数据  }&\cellcolor{\tableheadbgcolor}\textbf{ 帧尾  }\\\cline{1-4}
\endfirsthead
\hline
\endfoot
\hline
\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 帧头  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 数据\+ID  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 数据  }&\cellcolor{\tableheadbgcolor}\textbf{ 帧尾  }\\\cline{1-4}
\endhead
protocol\+\_\+header(4-\/byte)  &cmd\+\_\+id(2-\/byte)  &data (n-\/byte)  &frame\+\_\+tail(2-\/byte，\+CRC16，整包校验)  \\\cline{1-4}
\end{longtabu}



\begin{DoxyEnumerate}
\item 帧头详细定义
\end{DoxyEnumerate}

\tabulinesep=1mm
\begin{longtabu}spread 0pt [c]{*{4}{|X[-1]}|}
\hline
\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 帧头  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 偏移位置  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 字节大小  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 内容  }\\\cline{1-4}
\endfirsthead
\hline
\endfoot
\hline
\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 帧头  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 偏移位置  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 字节大小  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 内容  }\\\cline{1-4}
\endhead
sof(\+CMD)  &0  &1  &数据帧起始字节，固定值为 0x\+A5  \\\cline{1-4}
data\+\_\+length  &1  &2  &数据帧中 data 的长度  \\\cline{1-4}
crc\+\_\+check  &3  &1  &帧头\+CRC校验  \\\cline{1-4}
\end{longtabu}



\begin{DoxyEnumerate}
\item cmd\+\_\+id 命令码 ID 说明 {\bfseries{(字节偏移 4，字节大小 2)}}
\end{DoxyEnumerate}

\tabulinesep=1mm
\begin{longtabu}spread 0pt [c]{*{3}{|X[-1]}|}
\hline
\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 命令码  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 数据段长度  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 功能说明  }\\\cline{1-3}
\endfirsthead
\hline
\endfoot
\hline
\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 命令码  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 数据段长度  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 功能说明  }\\\cline{1-3}
\endhead
0x0001(可修改)  &2 byte (16-\/bit)  &视觉数据  \\\cline{1-3}
.......  &&\\\cline{1-3}
\end{longtabu}



\begin{DoxyEnumerate}
\item 数据段data (n-\/byte)
\end{DoxyEnumerate}

\tabulinesep=1mm
\begin{longtabu}spread 0pt [c]{*{4}{|X[-1]}|}
\hline
\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 数据  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 偏移位置  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 字节大小  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 内容  }\\\cline{1-4}
\endfirsthead
\hline
\endfoot
\hline
\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 数据  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 偏移位置  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 字节大小  }&\PBS\centering \cellcolor{\tableheadbgcolor}\textbf{ 内容  }\\\cline{1-4}
\endhead
flags\+\_\+register  &6  &2  &16位标志置位寄存器  \\\cline{1-4}
float\+\_\+data  &8  &4 \texorpdfstring{$\ast$}{*} data\+\_\+length  &float数据内容（4 \texorpdfstring{$\ast$}{*} data\+\_\+length-\/byte）  \\\cline{1-4}
\end{longtabu}



\begin{DoxyEnumerate}
\item frame\+\_\+tail(CRC16，整包校验)
\end{DoxyEnumerate}\hypertarget{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2master__machine_2_robf3db6ee153d24cdbb8a8c06ea0d4885b_autotoc_md64}{}\doxysection{\texorpdfstring{三、协议接口说明}{三、协议接口说明}}\label{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2master__machine_2_robf3db6ee153d24cdbb8a8c06ea0d4885b_autotoc_md64}

\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{comment}{/*更新发送数据帧，并计算发送数据帧长度*/}}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ get\_protocol\_send\_data(uint16\_t\ send\_id,\ \ \ \ \ \ \ \ \textcolor{comment}{//信号id}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint16\_t\ flags\_register,\ \textcolor{comment}{//\ 16位寄存器}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{float}\ *tx\_data,\ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//待发送的float数据}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint8\_t\ float\_length,\ \ \ \ \textcolor{comment}{//\ float的数据长度}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint8\_t\ *tx\_buf,\ \ \ \ \ \ \ \ \ \textcolor{comment}{//待发送的数据帧}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint16\_t\ *tx\_buf\_len);\ \ \ \textcolor{comment}{//待发送的数据帧长度}}

\end{DoxyCode}


将float\+\_\+data字段准备好放入{\ttfamily tx\+\_\+data\mbox{[}\mbox{]}}并设置好标志位{\ttfamily flags\+\_\+register}之后，调用此函数。它将计算循环冗余校验码，并把数据转换成串口发送的格式，放在{\ttfamily tx\+\_\+buf\mbox{[}\mbox{]}}中。


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{comment}{/*接收数据处理*/}}
\DoxyCodeLine{uint16\_t\ get\_protocol\_info(uint8\_t\ *rx\_buf,\ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//接收到的原始数据}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint16\_t\ *flags\_register,\ \textcolor{comment}{//接收数据的16位寄存器地址}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{float}\ *rx\_data);\ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//接收的float数据存储地址}}

\end{DoxyCode}


将收到的一包原始数据buff地址传入，若校验通过，会把收到的标志位和float数据解析出来，保存在{\ttfamily flags\+\_\+register\texorpdfstring{$\ast$}{*}}和 {\ttfamily rx\+\_\+data\mbox{[}\mbox{]}}中。 